package com.yuandengta.yomall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.yuandengta.yomall.product.entity.AttrEntity;
import com.yuandengta.yomall.product.service.AttrAttrgroupRelationService;
import com.yuandengta.yomall.product.service.AttrService;
import com.yuandengta.yomall.product.service.CategoryService;
import com.yuandengta.yomall.product.vo.AttrGroupRelationVO;
import com.yuandengta.yomall.product.vo.AttrGroupWithAttrsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yuandengta.yomall.product.entity.AttrGroupEntity;
import com.yuandengta.yomall.product.service.AttrGroupService;
import com.yuandengta.common.utils.PageUtils;
import com.yuandengta.common.utils.R;



/**
 * 属性分组
 *
 * @author kevin
 * @email 178676392@qq.com
 * @date 2020-12-20 17:30:25
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    /**
     * 获取分类下所有分组及关联的所有属性
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupWithAttrsVO> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data", vos);
    }

    /**
     * 查询属性分组与属性关联关系
     * @param attrgroupId 属性分组ID
     * @return 当前属性分组的属性集合
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> attrEntities = attrService.getAttrRelation(attrgroupId);
        return R.ok().put("data", attrEntities);
    }

    /**
     * 删除属性关联关系
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVO[] vos) {
        attrService.deleteRelation(vos);
        return R.ok();
    }

    /**
     * 查询属性分组未关联的属性
     * @return 没有关联分组的属性集合
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getNoAttrRelation(params, attrgroupId);
        return R.ok().put("page", page);
    }

    /**
     * 新增属性与分组关联关系
     * @param vos
     * @return
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVO> vos) {
        relationService.addRelation(vos);
        return R.ok();
    }

    /**
     * 根据三级分类ID查询父类下的属性分组
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable Long catelogId){
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }

    /**
     * 查询属性分组
     * @param attrGroupId 属性分组ID
     * @return 属性分组信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        // 查找三级分类的完整路径 [level1Id, level2Id, level3Id]
        Long[] catelogPath = categoryService.findCategoryPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(catelogPath);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
